package com.zqc.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zqc.common.R;
import com.zqc.entity.Employee;
import com.zqc.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    public EmployeeService employeeService;
    private R<Employee> success;
    private R<Employee> success1;

    /**
     * 后台登录
     *
     * @param req
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest req, @RequestBody Employee employee) {
        // 1 加密密码
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        // 2 查询数据库库
        Employee emp = employeeService.getOne(
                new LambdaQueryWrapper<Employee>().eq(Employee::getUsername, employee.getUsername()));
        // 3 判断查询结果
        if (emp == null) {
            return R.error("用户名错误");
        }

        // 4 判断密码是否正确
        if (!emp.getPassword().equals(password)) {
            return R.error("密码错误");
        }

        // 5 判断账号状态
        if (emp.getStatus() == 0) {
            return R.error("该账号已禁用");
        }
        // 6 将此账号id保存到session中
        req.getSession().setAttribute("employee", emp.getId());
        return R.success(emp);
    }

    /**
     * 后台退出
     *
     * @param req
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest req) {
        // 清除session数据
        req.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    @PostMapping
    public R<String> save(HttpServletRequest request, @RequestBody Employee employee) {
        // 1 设置初始值密码，并加密
        String password = DigestUtils.md5DigestAsHex("666666".getBytes());
        employee.setPassword(password);

        // 公共字段在MyMetaObjectHandler类中进行了统一注入
        // 2 设置创建时间，更新时间
        //employee.setCreateTime(LocalDateTime.now());
        //employee.setUpdateTime(LocalDateTime.now());
        // 3 设置创建人
        //Long employeeId = (Long) request.getSession().getAttribute("employee");
        //employee.setCreateUser(employeeId);
        //employee.setUpdateUser(employeeId);
        // 4 保存到数据库
        employeeService.save(employee);
        return R.success("添加员工成功");
    }

    /**
     * 员工列表分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
        // 创建分页构造器
        Page<Employee> employeePage = new Page<>(page, pageSize);
        // 查询数据
        employeeService.page(employeePage, new LambdaQueryWrapper<Employee>()
                .like(StringUtils.isNotEmpty(name), Employee::getName, name)
                .orderByAsc(Employee::getUpdateTime));
        return R.success(employeePage);
    }

    @PutMapping
    public R<String> update(HttpServletRequest request, @RequestBody Employee employee) {
        // 设置更新时间、更新人
        //employee.setUpdateTime(LocalDateTime.now());
        //employee.setUpdateUser((Long) request.getSession().getAttribute("employee"));

        // 更新员工状态
        employeeService.updateById(employee);
        return R.success("修改成功");
    }

    /**
     * 获取单个员工信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> one(@PathVariable(name = "id") Long id) {
        Employee employee = employeeService.getById(id);
        if (employee == null) {
            return R.error("没有查询到该员工");
        }

        return R.success(employee);

    }
}
